<form action="${CONTEXT_PATH}/user/address/save" method="post" id="users_address_add" class="form-horizontal">
<input type="hidden" name="m.useruuid" value="${m.uuid}"/>
<#include "/WEB-INF/template/page/admin/user/address/_form.ftl"/>
</form>
<script type="text/javascript">
    jQuery("#users_address_add").ready(function () {
        $("#province").change(function(){
            nextselect("province","city");
        }).click(selectclick);
        $("#city").change(function(){
            nextselect("city","area");
        }).click(selectclick);
        $("#area").change(function(){
            nextselect("area","street");
        }).click(selectclick);
        $("#street").change(function(){
            nextselect("street","");
        }).click(selectclick);
        nextselect("","province");


        // 定义验证参数
        var validateOptions = $.extend(CONST_VALIDATE.OPTION, {
            rules: {
                "m.rankname": {
                    required: true
                },
                "m.rankstart": {
                    required: true,
                    digits:true,
                    min:0
                },
                "m.rankend": {
                    required: true,
                    digits:true,
                    min:0
                }
            },
            messages: {
                "m.rankname": {
                    required: "<i class='fa fa-exclamation tooltips'></i>等级名称(见习女神,男神,xxx)",
                    maxlength: $.validator.format("<i class='fa fa-exclamation tooltips'></i>登录ID不能大于{0}个字符")
                },
                "m.rankstart": {
                    required: "<i class='fa fa-exclamation tooltips'></i>请输入开始阀值",
                    digits: "<i class='fa fa-exclamation tooltips'></i>请输入大于0的整数",
                    min: $.validator.format("<i class='fa fa-exclamation tooltips'></i>阀值不能小于{0}")
                },
                "m.rankend": {
                    required: "<i class='fa fa-exclamation tooltips'></i>请输入开始阀值",
                    digits: "<i class='fa fa-exclamation tooltips'></i>请输入大于0的整数",
                    min: $.validator.format("<i class='fa fa-exclamation tooltips'></i>阀值不能小于{0}")
                }
            },
            submitHandler:function(form) {
                var modal = $("#ajax_add");
                $(form).ajaxSubmit(function (data, status, xhr) {
                    if (data.status) {
                        toastr.success(data.msg, '操作提示');
                    } else {
                        toastr.error(data.msg, '操作提示');
                    }
                });
                $(modal).modal('hide');
                dtgrid.load();
            }
        });
        //表单验证
        $("#users_address_add").validate(validateOptions);
    });
    /**
     * 1.清空其余无相关select
     * 2.设置hidden
     * @param e
     */
    function selectclick(e){
        var o=$("#"+e.target.id);
        //设置hidden
        o.next().val(o.find("option:selected").text());
        //清空当前select 第二个元素后的select
        o.nextAll("select:gt(0)").empty().nextAll().empty();
        //重置preview
        appendTo();
        //当前元素有1个时,调用onchange
        if(o.size()==1){
            o.change();
        }
    }
    function nextselect(sid,did){
        var p=$("#"+sid).val() || 0;
        $.getJSON("${CONTEXT_PATH}/user/address/get/"+p,function(d){
            if(d.status){
                var obj=$("#"+did);
                obj.empty();
                $.each(d.dataList,function(i,a){
                    obj.append("<option value="+this.id+">"+this.areaname+"</option>");
                });
                appendTo();
            }
        })
    }

    function appendTo(){
        var v1=$("#province option:selected").text();
        var v2=$("#city option:selected").text();
        var v3=$("#area option:selected").text();
        var v4=$("#street option:selected").text();
        $("#address_preview").empty().append(v1+" "+v2+" "+v3+" "+v4);
    }
</script>